//
//  InspectionInputCell.swift
//  Employee
//
//  Created by swordjoy on 2017/3/31.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import IQKeyboardManager

enum InspectionInputCellType {
  
  case text
  case number
  
}

class InspectionInputCell: UITableViewCell {
  
  static var identifier = "InspectionInputCell.swordjoy"
  
  var name: String = "" {
    
    didSet { titleLabel.text = name }
    
  }
  
  var content: String = "" {
    
    didSet {
      textView.textView.text = content
      if content.isEmpty {
        textView.placeholderLabel.isHidden = false
      } else {
        textView.placeholderLabel.isHidden = true
      }
    }
    
  }
  
  var isFlow: Bool = false {
    didSet {
      if isFlow {
        contentV.snp.updateConstraints({ (make) in
          make.left.right.top.bottom.equalToSuperview()
        })
      }
    }
  }
  
  var getSelect: ( (String, String) -> () )?
  
  var type: InspectionInputCellType = .text {
    didSet {
      
      if .text == type {
        textView.textView.keyboardType = .default
        textView.placeholderLabel.text = "点击输入文字"
      }
      else if .number == type {
        textView.textView.keyboardType = .numberPad
        textView.placeholderLabel.text = "点击输入数字"
      }
      
    }
  }
  
  fileprivate var contentV: UIView!
  
  fileprivate var titleLabel: UILabel!
  fileprivate var textView: PlaceholderTextView!
  
  fileprivate var sameLevelPicker: SJSameLevelPicker?
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    
    backgroundColor = UIColor.clear
    selectionStyle = .none
    
    create()
    
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

}

extension InspectionInputCell: UITextViewDelegate {
  
  func textViewDidBeginEditing(_ textView: UITextView) {
    
    self.textView.placeholderLabel.isHidden = true
    
    if .text == type {
    }
    else if .number == type {
    
    }
    
    
  }
  
  func textViewDidEndEditing(_ textView: UITextView) {
    self.textView.placeholderLabel.isHidden = !textView.text.isEmpty
  }
  
  func textViewDidChange(_ textView: UITextView) {
    
    self.textView.placeholderLabel.isHidden = !textView.text.isEmpty
    
    guard let closure = getSelect else { return }
    closure(name, textView.text)
    
  }
  
}

// MARK: - fileprivate

extension InspectionInputCell {
  
  
  
  fileprivate func create() {
    
    contentV = UIView()
    contentView.addSubview(contentV)
    contentV.backgroundColor = UIColor.white
    contentV.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.right.equalTo(-10)
      make.top.equalTo(5)
      make.bottom.equalTo(-5)
    }
    contentV.layer.cornerRadius = 4
    contentV.clipsToBounds = true
    
    titleLabel = UILabel()
    contentV.addSubview(titleLabel)
    titleLabel.font = GlobalConfig.system16Font
    titleLabel.textColor = GlobalConfig.TextDeepColor
    titleLabel.numberOfLines = 0
    titleLabel.snp.makeConstraints { (make) in
      make.left.top.equalTo(20)
      make.right.equalTo(-20)
    }
    
    textView = PlaceholderTextView()
    contentV.addSubview(textView)
    textView.textView.font = GlobalConfig.system15Font
    textView.isOldLevel = true
    textView.textView.textColor = GlobalConfig.BGSkyBlueColor
    textView.placeholderLabel.font = GlobalConfig.system15Font
    textView.placeholderLabel.textColor = GlobalConfig.TextLightColor
    textView.textView.delegate = self
    textView.snp.makeConstraints { (make) in
      make.top.equalTo(titleLabel.snp.bottom).offset(20)
      make.left.equalTo(40)
      make.right.equalTo(-40)
      make.bottom.equalTo(20)
    }

  }
}
